package com.zsc.edu.testnetwork;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btn_req_url = findViewById(R.id.btn_req_url);
        btn_req_url.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                // 初始化 子线程
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            // url
                            //URL url = new URL("https://www.zsc.edu.cn");
                            URL url = new URL("http://172.16.56.250:8000/api/hotnews");

                            // url 连接
                            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                            // 请求方法，如果上传可用 POST
                            connection.setRequestMethod("GET");
                            // 连接超时
                            connection.setConnectTimeout(3000);
                            // 读取超时
                            connection.setReadTimeout(3000);
                            // 获得输入流
                            InputStream inputStream = connection.getInputStream();
                            // 输入流转成BufferedReader
                            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

                            String line;
                            StringBuilder response = new StringBuilder();
                            // 一次读取一行
                            while((line = reader.readLine()) != null){
                                // 每一行连接起来
                                response.append(line);
                            }

                            Log.d("---MainActivity---", "ResponseCode: " + connection.getResponseCode());
                            Log.d("---MainActivity---", "ResponseMessage: " + connection.getResponseMessage());
                            Log.d("---MainActivity---", "response: " + response);

                            // 请求结果，字符串转成JSON对象
                            JSONObject jsonObject = new JSONObject(response.toString());
                            // 根据key，获得 value，是一个JSON数组
                            JSONArray hotnews = jsonObject.getJSONArray("hotnews");
                            // 遍历数组
                            for (int i=0; i<hotnews.length(); ++i){
                                // 取出每一条新闻，也是一个JSON对象
                                JSONObject hotnew = hotnews.getJSONObject(i);
                                // 根据key，按照不同类型，获得 value
                                String news = hotnew.getString("news");
                                int hot = hotnew.getInt("hot");

                                // TODO 完成其它数据的解析
                                // TODO Log 解析的数据


//                                "id": 6,
//                                "news": "如何看待台湾当局称大陆纪念抗美援朝，是在「用战争威胁台湾」?",
//                                "img": "img/img7.png",
//                                "hot": 12,
//                                "share": 80

                                int id = hotnew.getInt("id");
                                int share = hotnew.getInt("share");
                                String img = hotnew.getString("img");

                                String msg = "id: " + id + " 新闻: " + news + " 图片: " + img + " 热度: " + hot + " 分享: " + share ;
                                Log.d("---MainActivity---", msg);

                            }

                            // 回到UI主线程
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    // 只有UI主线程能够操作，由UI主线程创建的 TextView
                                    TextView tv = findViewById(R.id.tv_content);
                                    tv.setText(response);
                                }
                            });



                        } catch (IOException e) {
                            e.printStackTrace();
                        }catch(JSONException e){
                            e.printStackTrace();
                        }
                    }
                });
                // 启动子线程
                thread.start();




            }
        });


        Button btn_req_img = findViewById(R.id.btn_req_img);
        btn_req_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 初始化 子线程
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            // url
                            //URL url = new URL("https://www.zsc.edu.cn");
                            URL url = new URL("http://172.16.56.250:8000/api/img/img1.png");

                            // url 连接
                            HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                            // 请求方法，如果上传可用 POST
                            connection.setRequestMethod("GET");
                            // 连接超时
                            connection.setConnectTimeout(3000);
                            // 读取超时
                            connection.setReadTimeout(3000);
                            // 获得输入流
                            InputStream inputStream = connection.getInputStream();

                            // 输入流 转 位图 Bitmap
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                            // 回到UI主线程
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    // 只有UI主线程能够操作，由UI主线程创建的 ImageView
                                    ImageView image_view = findViewById(R.id.image_view);
                                    image_view.setImageBitmap(bitmap);
                                }
                            });
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                });
                // 启动子线程
                thread.start();
            }
        });

    }
}